<template>
  <div>
        <common-head></common-head>
    <!-- 下拉刷新列表 -->
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh" success-text="刷新成功">
      <!-- 具体列表内容 -->
      <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
        <!-- 卡片模板 -->
        <van-cell v-for="item in list" :key="item.id" class="vanCell" @click="toNewDetail(item.id)">
          <div class="van-card">
            <div class="van-card__header">
              <!-- 左侧图片 -->
              <a class="van-card__thumb" style="width: 42px; height: 42px;">
                <div class="van-image">
                  <img :src="item.img_url" class="van-image__img" style="object-fit: cover;" />
                </div>
              </a>
              <div class="van-card__content">
                <!-- 标题 -->
                <div>
                  <div class="van-card__title">{{item.title}}</div>
                </div>
                <!-- 底部文字区域 -->
                <div class="van-card__bottom">
                  <div class="van-card__price">
                    <div>
                      <span class="van-card__price-decimal">发表时间: {{item.add_time.substr(0,10)}}</span>
                    </div>
                  </div>
                  <div class="van-card__num">点击: {{item.click}}</div>
                </div>
              </div>
            </div>
          </div>
        </van-cell>
      </van-list>
    </van-pull-refresh>

    <common-footer></common-footer>
  </div>
</template>

<script>
export default {
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      refreshing: false
    }
  },
  mounted () {
    this.getNewsList()
  },
  methods: {
    //   获取图文资讯
    async getNewsList () {
      const { data: res } = await this.$http.get('/api/getnewslist')
      // 获取数据失败
      if (res.status === 1) this.$toast('获取图文资讯失败！')
      this.$toast('获取图文资讯成功！')
      this.list = res.message
    },
    // 列表即将滚动到底部时，加载更多。
    onLoad () {
      setTimeout(() => {
        if (this.refreshing) {
          this.list = []
          this.refreshing = false
        }
        // 获取新数据
        this.getNewsList()
        this.loading = false

        if (this.list.length >= 8) {
          this.finished = true
        }
      }, 1000)
    },
    // 下拉刷新
    onRefresh () {
      // 清空列表数据
      this.finished = false

      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true
      this.onLoad()
    },
    // 跳转到新闻详情页面
    toNewDetail (id) {
      this.$router.push(`/newslist/detail?id=${id}`)
    }
  }
}
</script>

<style lang="less" scoped>
.van-cell {
  padding: 0 10px;
}
.van-card {
  padding: 10px;
  background-color: #fff;
}
.van-card__title {
  font-weight: 700;
  font-size: 14px;
  overflow: hidden;
  /*文本不会换行*/
  white-space: nowrap;
  /*当文本溢出包含元素时，以省略号表示超出的文本*/
  text-overflow: ellipsis;
}
.van-image__img {
  width: 42px;
  height: 42px;
  border-radius: 0;
  margin-right: 10px;
}
.van-card__content {
  min-height: 0px;
}
.van-card__bottom {
  margin-top: 9px;
}
.van-card__price-decimal,
.van-card__num {
  color: #226aff;
}

.van-pull-refresh {
  margin-top: 50px;
}
</style>
